/*********************************************************************************
 *      Copyright:  (C) 2024 LiYi<1751425323@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  db_sqlite3.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(25/04/24)
 *         Author:  LiYi <1751425323@qq.com>
 *      ChangeLog:  1, Release initial version on "25/04/24 16:48:32"
 *                 
 ********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sqlite3.h"

void sqlite_tem(char *buf)
{  
	int             nrow=0;
	int             ncolumn = 0;
	char          **azResult=NULL;
	int             rv; 
	sqlite3        *db=NULL;
	char           *zErrMsg = 0;
	char            sql1[100];
	char           *ipaddr=NULL;
	char           *datetime=NULL;
	char           *temper=NULL;
	char           *sql = "create table if not exists temperature(ipaddr char(30), datetime char(50), temper  char(30))"; 

	ipaddr = strtok(buf,"/");
	datetime = strtok(NULL, "/");
	temper = strtok(NULL, "/");

	rv = sqlite3_open("tempreture.db", &db);
	if(rv)
	{
		printf("Can't open database:%s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return;
	}
	printf("opened a sqlite3 database named tempreture.db successfully!\n");

	int ret = sqlite3_exec(db,sql, NULL, NULL, &zErrMsg);
	if(ret != SQLITE_OK)
	{
		printf("create table fail: %s\n",zErrMsg);
	}

	if(snprintf(sql1,sizeof(sql1), "insert into temper values('%s','%s','%s')", ipaddr, datetime, temper) < 0)
	{
		printf("Failed to write data\n");
	}
	printf("Write data successfully!\n");

	sqlite3_exec(db, sql1, 0, 0, &zErrMsg);
	sqlite3_free(zErrMsg);
	sqlite3_close(db);
	return;
}



